package controller

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"os"
)

/*
@author：zjy
@Emial:734219310@qq.com
*/
type OneChartData struct {
	OneJsons []OneJson `json:"chartData"`
}

type OneJson struct {
	Title string `json:"title"`
	Num   int    `json:"num"`
}
type TwoChartData struct {
	TwoJson []TwoJson `json:"chartData"`
}

type TwoJson struct {
	Name  string `json:"name"`
	Value int    `json:"value"`
}

func EchartOne(c *gin.Context) {
	jsonFile, err := os.Open("dataEchart/one.json")
	if err != nil {
		fmt.Println("error opening json file")
		return
	}
	defer jsonFile.Close()
	jsonData, err := ioutil.ReadAll(jsonFile)
	if err != nil {
		fmt.Println("error reading json file")
		return
	}
	var one OneChartData
	json.Unmarshal(jsonData, &one)
	c.JSON(200, gin.H{
		"code":    200,
		"message": "success",
		"data":    one.OneJsons,
	})
}

func EchartTwo(c *gin.Context) {
	jsonFile, err := os.Open("dataEchart/two.json")
	if err != nil {
		fmt.Println("error opening json file")
		return
	}
	defer jsonFile.Close()
	jsonData, err := ioutil.ReadAll(jsonFile)
	if err != nil {
		fmt.Println("error reading json file")
		return
	}
	var two TwoChartData
	json.Unmarshal(jsonData, &two)
	c.JSON(200, gin.H{
		"code":    200,
		"message": "success",
		"data":    two.TwoJson,
	})
}
func EchartMap(c *gin.Context) {
	jsonFile, err := os.Open("dataEchart/map.json")
	if err != nil {
		fmt.Println("error opening json file")
		return
	}
	defer jsonFile.Close()
	jsonData, err := ioutil.ReadAll(jsonFile)
	if err != nil {
		fmt.Println("error reading json file")
		return
	}
	c.JSON(200, gin.H{
		"code":    200,
		"message": "success",
		"data":    jsonData,
	})
}
